package com.quan.junit.test;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.quan.commons.utils.DateUtils;
import com.quan.hr.entity.HrCalendar;

import java.util.*;


public class CalendarTest2 extends ApplicationTests {

    public static void main(String[] args) {
        int year = 2023;
        int month = 12;

        initCalendar(year);

    }

    public static void initCalendar(Integer year) {
        List<HrCalendar> list = new ArrayList<>();
        for (int month = 1; month <= 12; ++month) {
            list.addAll(initCalendarList(year, month));
        }

        //final List<PmgHrCalendar> collect = list.stream().map(item -> {
        //    System.out.println(item);
        //    return item;
        //}).collect(Collectors.toList());


        Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>)getHoliday(year);
        Set<String> strings = holiday.keySet();
        for (String str : strings) {
            Map<String, Object> stringObjectMap = holiday.get(str);

            String date = (String) stringObjectMap.get("date");
            String name = (String) stringObjectMap.get("name");
            Boolean holidy = (Boolean) stringObjectMap.get("holiday");
            String target = (String) stringObjectMap.get("target");

            System.out.println(date+"=="+name+"=="+holidy+"=="+(target==null?"":target));
            System.out.println(stringObjectMap);

        }
    }

    public void initCalendar(Integer year, Integer month) {
        final List<HrCalendar> list = initCalendarList(year, month);
    }

    public static List<HrCalendar> initCalendarList(int year, int month) {
        List<HrCalendar> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();

        // 获取月份的最后一天，用于迭代月份的每一天
        calendar.set(year, month, 0);
        final int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (int day = 1; day <= maxDay ; day++) {
            calendar.set(year, month - 1, day);
            int week = calendar.get(Calendar.DAY_OF_WEEK);

            HrCalendar entity = new HrCalendar();
            entity.setCalendarDate(DateUtils.UDateToLocalDate(calendar.getTime()));
            entity.setCalendarTitle(getWeek(calendar));
            entity.setWeekend(week == 1 || week == 7 ? 1 : 0 );


            list.add(entity);
        }
        return list;
    }

    private static String getWeek(Calendar date) {
        final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
        return dayNames[dayOfWeek - 1];
    }

    /**
     * 获取国家节假日
     * @return
     */
    private static Object getHoliday(int year) {
        String url = "http://timor.tech/api/holiday/year/"+year+"/";

        final String res = HttpUtil.get(url);
        final Map map = JSONObject.parseObject(res, LinkedHashMap.class);

        Integer code = (Integer) map.get("code");
        if (code != 0) {
            return null;
        }
        map.get("holiday");

        Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) map.get("holiday");
        return map.get("holiday");
    }


}
